package com.cskaoyan.service.wx_cart;

import com.cskaoyan.bean.wx_cart.Cart;
import com.cskaoyan.bean.wx_cart.CartExample;
import com.cskaoyan.bean.wx_cart.vo.CartIndexVo;
import com.cskaoyan.bean.wx_cart.wx_goods.Goods;
import com.cskaoyan.bean.wx_cart.wx_goods.GoodsExample;
import com.cskaoyan.bean.wx_cart.wx_goods.Product;
import com.cskaoyan.exception.wx_cart.CartEmptyException;
import com.cskaoyan.exception.wx_cart.GoodsEmptyException;
import com.cskaoyan.mapper.wx_cart.CartMapper;
import com.cskaoyan.mapper.wx_cart.wx_goods.GoodsMapperByWxCart;
import com.cskaoyan.mapper.wx_cart.wx_goods.ProductMapperByWxCart;
import com.cskaoyan.utils.ReflectionSetJavaBeanUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 类<code>Doc</code>用于：TODO
 *
 * @author Acher
 * @version 1.0
 * @date 2021-10-22
 */
@Service
public class WxCartService2Impl implements WxCartService2 {

    @Autowired
    CartMapper cartMapper;
    @Autowired
    GoodsMapperByWxCart goodsMapperByWxCart;
    @Autowired
    ProductMapperByWxCart productMapperByWxCart;

    @SneakyThrows
    @Override
    public CartIndexVo index() {
        // 购物车内商品数
        // 是否check的商品数
        // 购物车总价
        // 未知值
        // 获取购物车内商品信息
//        CartExample example = new CartExample();
        List<Cart> cartList = cartMapper.selectByExample(new CartExample());
        if (cartList == null || cartList.size() == 0)   throw new CartEmptyException("购物车还是空的，快去买买买吧~");
        int goodsCount = cartList.size();
        int checkedGoodsCount = 0;
        double goodsAmount = 0.0;
        int checkedGoodsAmount = 0;
        List<Cart> carts = new ArrayList<>();
        for (Cart cart : cartList) {
            if (cart.getChecked())  checkedGoodsCount++;
            goodsAmount += cart.getPrice().doubleValue();
            if (!cart.getDeleted()) carts.add(cart);
        }
        return new CartIndexVo(new CartIndexVo.CartTotalDTO(
                goodsCount,checkedGoodsCount,goodsAmount,checkedGoodsAmount
        ), carts);
    }

    @Override
    public boolean update(Cart cart) {
        // 根据购物车id更新商品信息
        return cartMapper.updateByPrimaryKeySelective(cart) == 1;
    }

    @SneakyThrows
    @Override
    public int add(Cart cart) {
        ReflectionSetJavaBeanUtils.setJavaBean(cart);
        // 根据product查是否存在相同规格商品，如果存在，则直接增加number
        CartExample example1 = new CartExample();
        Integer productId = cart.getProductId();
        example1.createCriteria().andProductIdEqualTo(productId);
        // 可能会抛异常
        int affectedRows = cartMapper.updateByExampleSelective(cart, example1);
        // goodsName
        // 不存在，则添加购物车商品
        CartExample cartExample3 = new CartExample();
        cartExample3.createCriteria().andDeletedEqualTo(false);
        if (affectedRows != 0)
            return (int) cartMapper.countByExample(cartExample3);
        // 根据goodsId查goods表的goods_sn、name、retail_price、pic_url
        Goods goods = goodsMapperByWxCart.selectByPrimaryKey(cart.getGoodsId());
        if (goods == null)
            throw new GoodsEmptyException("该商品不存在");
        // 根据productId查goods_product表的specifications数组
        Product product = productMapperByWxCart.selectByPrimaryKey(productId);
        if (product == null)
            throw new GoodsEmptyException("该商品不存在");
        cart.setGoodsSn(goods.getGoodsSn());
        cart.setGoodsName(goods.getName());
        cart.setPrice(goods.getRetailPrice());
        cart.setPicUrl(goods.getPicUrl());
        cart.setSpecifications(product.getSpecifications());
        cartMapper.insert(cart);
        // 返回的是商品的种类
        return (int) cartMapper.countByExample(cartExample3);
    }

    @Override
    public int count() {
        // 查询购物车内商品数
        CartExample example = new CartExample();
        example.createCriteria().andDeletedEqualTo(false);
        return (int) cartMapper.countByExample(example);
    }
}
